package com.example.administrator.orchardeveryday.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.administrator.orchardeveryday.BaseApp;
import com.example.administrator.orchardeveryday.R;
import com.example.administrator.orchardeveryday.ui.LoginActivity;
import com.example.administrator.orchardeveryday.utils.ImageLoaderHelper;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends Fragment implements View.OnClickListener{

    DisplayImageOptions displayImage;
    private int GET_USER_FACE=1;
    private int[] images={R.mipmap.my_order,R.mipmap.my_comment,R.mipmap.mycenter_collect_sel,
                          R.mipmap.my_gift,R.mipmap.my_privilege,R.mipmap.my_trial};
    private String[] names={"我的订单","评论晒单","我的关注","我的赠品","我的特权","我的试吃"};
    private GridView gv;
    private TextView login;
    private TextView register;
    private TextView afterLogin;
    private ImageView userface;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        displayImage = ImageLoaderHelper.getDisplayImageLoader();
        View view=inflater.inflate(R.layout.fragment_mine, container, false);
        initView(view);
        
        return view;
    }

    private void initView(View view) {
        gv= (GridView) view.findViewById(R.id.fragment_mine_gv);
        login= (TextView) view.findViewById(R.id.fragment_mine_login_tv);
        register= (TextView) view.findViewById(R.id.fragment_mine_register_tv);
        afterLogin= (TextView) view.findViewById(R.id.fragment_mine_afterlogin_tv);
        userface= (ImageView) view.findViewById(R.id.fragment_mine_userface_iv);
        login.setOnClickListener(this);
        register.setOnClickListener(this);
        SimpleAdapter gridAdapter = new SimpleAdapter(getActivity(), initGridData(),
                R.layout.item_mine_fragment_gridview, new String[]{"img", "name"},
                new int[]{R.id.item_mine_fragment_content_img, R.id.item_mine_fragment_content_tv});
        gv.setAdapter(gridAdapter);
    }

    private List initGridData() {
        ArrayList<HashMap<String, Object>> gridList = new ArrayList<>();
        for (int i = 0; i < images.length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("img", images[i]);
            map.put("name", names[i]);
            gridList.add(map);
        }
        return gridList;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==GET_USER_FACE)
        {

            if(data!=null)
            {
                String imgUrl=data.getStringExtra("userFace");
                ImageLoader.getInstance().displayImage(imgUrl.toString(), userface, displayImage);
                login.setVisibility(View.GONE);
                register.setVisibility(View.GONE);
                afterLogin.setText(BaseApp.user.getName());
            }

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.fragment_mine_login_tv:
                Intent intent=new Intent(getActivity(), LoginActivity.class);
                startActivityForResult(intent, GET_USER_FACE);
                break;
            case R.id.fragment_mine_register_tv:

                break;
        }
    }
}
